1 00:00:00,630 --> 00:00:00,960 All right. 2 00:00:00,960 --> 00:00:03,990 In this video, I'm going to continue with my steps. 3 00:00:03,990 --> 00:00:08,550 And in so doing, I'm going to go over return values for functions. 4 00:00:08,550 --> 00:00:13,290 And I'm also going to do a little loop at the end of this to make a whole bunch of steps with very little 5 00:00:13,290 --> 00:00:13,890 code. 6 00:00:13,890 --> 00:00:18,510 I'm going to do a whole video on loops, but I thought a quick introduction would be helpful. 7 00:00:18,510 --> 00:00:25,260 So let's go ahead and stop this, open the workspace and you will see a single script in here. 8 00:00:25,260 --> 00:00:27,380 There's only a single script in the whole game. 9 00:00:27,390 --> 00:00:28,860 I'll put it in the description. 10 00:00:28,860 --> 00:00:32,550 That way, if you don't have it, you just copy it from the description. 11 00:00:32,550 --> 00:00:36,900 And then if you don't have the last video or something like that, you can start from here. 12 00:00:37,170 --> 00:00:37,560 Right? 13 00:00:37,560 --> 00:00:44,310 So I'm going to add, I have this make step function right here where we make a step and then we call 14 00:00:44,310 --> 00:00:45,060 it down here. 15 00:00:45,060 --> 00:00:45,210 Right? 16 00:00:45,240 --> 00:00:48,090 We call it a few times, four times. 17 00:00:48,090 --> 00:00:56,490 And I have a requirement from whoever told me that I got to do this, that we need to get the part outside 18 00:00:56,490 --> 00:01:01,200 of the function and change it in some way, maybe change the material. 19 00:01:01,200 --> 00:01:01,560 Right. 20 00:01:01,560 --> 00:01:04,800 But we can't use the part down here because it's local to our function. 21 00:01:05,190 --> 00:01:07,110 Let's go ahead and fix that. 22 00:01:07,110 --> 00:01:10,380 Let's just say return part. 23 00:01:10,410 --> 00:01:15,130 Now, when we get to the end of this function, we're going to return that part that we created and 24 00:01:15,150 --> 00:01:16,830 we can get it down here. 25 00:01:16,830 --> 00:01:22,630 We could do something like this, we could say local, we call it part, but I think I'm going to call 26 00:01:22,650 --> 00:01:23,970 P one. 27 00:01:24,480 --> 00:01:25,050 Right? 28 00:01:25,050 --> 00:01:26,730 And then we have make step. 29 00:01:27,300 --> 00:01:28,650 Should we get rid of these? 30 00:01:28,650 --> 00:01:30,150 We can add those later. 31 00:01:30,180 --> 00:01:32,250 We'll just work on one call for now. 32 00:01:32,460 --> 00:01:34,440 This is the part, right? 33 00:01:34,440 --> 00:01:35,970 So we call make step. 34 00:01:36,060 --> 00:01:40,470 It does all of its stuff, it returns the part, plops it into p one. 35 00:01:41,010 --> 00:01:41,730 Cool. 36 00:01:41,730 --> 00:01:50,520 Now I can change things pe one material and then we need that enum to assign the material right in m 37 00:01:51,150 --> 00:01:56,070 we have a material section in the E names and then we have diamond plate. 38 00:01:56,790 --> 00:01:59,220 That's a perfect step material, right? 39 00:01:59,250 --> 00:02:00,390 Diamond plate. 40 00:02:00,390 --> 00:02:04,260 Let's go ahead and see if it's actually being changed to diamond plate. 41 00:02:04,260 --> 00:02:07,980 Unfortunately, we're only going to have one step now because I deleted the other ones. 42 00:02:09,770 --> 00:02:10,610 There it is. 43 00:02:10,610 --> 00:02:13,880 And it is, in fact, diamond plated. 44 00:02:14,270 --> 00:02:15,320 Pretty cool. 45 00:02:15,920 --> 00:02:17,290 Let's take a look at it again. 46 00:02:17,300 --> 00:02:19,340 Let's go back steps. 47 00:02:20,060 --> 00:02:21,410 So something you should know. 48 00:02:21,410 --> 00:02:25,170 In Lua you can return more than one value from a function, right? 49 00:02:25,190 --> 00:02:27,020 Like a job, you can only return one thing. 50 00:02:27,020 --> 00:02:31,130 So if you need several things from the function, you have to return a data structure here. 51 00:02:31,130 --> 00:02:33,590 You could just return them separately. 52 00:02:33,590 --> 00:02:42,260 Or I should say individually, I'm going to get the color from the random brick color and return it 53 00:02:42,260 --> 00:02:45,050 as a string as the name of the color. 54 00:02:45,050 --> 00:02:57,770 So I'm going to say local color name, let's say brick color, au part brick color part dot brick color 55 00:02:58,640 --> 00:02:59,630 with a B. 56 00:02:59,720 --> 00:03:00,320 There you go. 57 00:03:00,610 --> 00:03:01,190 Name. 58 00:03:01,460 --> 00:03:02,900 That was a little harder than I thought. 59 00:03:03,920 --> 00:03:04,130 All right. 60 00:03:04,130 --> 00:03:06,420 So this is going to have the name of the brick color. 61 00:03:06,440 --> 00:03:07,580 We're going to copy it. 62 00:03:07,580 --> 00:03:14,180 And then I'll just do a comma and paste color name. 63 00:03:14,180 --> 00:03:16,900 So color name is going to get returned with part. 64 00:03:16,910 --> 00:03:19,580 How do we capture it or how do we just go down here? 65 00:03:20,390 --> 00:03:22,760 Same way we returned it, comma. 66 00:03:24,640 --> 00:03:29,400 Her name so local will apply to both p one and colour name. 67 00:03:29,400 --> 00:03:30,070 Oops I messed up. 68 00:03:30,090 --> 00:03:32,600 PE one control z and colour name. 69 00:03:32,610 --> 00:03:41,100 Let's print this out to make sure that we did in fact get the colour name, print colour name equals 70 00:03:41,550 --> 00:03:45,090 and what I call it colour name. 71 00:03:45,390 --> 00:03:47,700 I was very creative like that colour name. 72 00:03:47,700 --> 00:03:49,650 And you could call this something different, right? 73 00:03:49,650 --> 00:03:51,570 Just so that you know, it doesn't have to match. 74 00:03:51,570 --> 00:03:53,090 Just like I did here with PE one. 75 00:03:53,100 --> 00:03:56,030 You could call the C name, right? 76 00:03:56,040 --> 00:04:00,450 Keep in mind that is a capital N, let's call that C name. 77 00:04:02,400 --> 00:04:03,070 Cool. 78 00:04:03,090 --> 00:04:04,260 Let's go ahead and play it. 79 00:04:04,290 --> 00:04:07,920 Let's get our view output window so we can see it print out. 80 00:04:07,950 --> 00:04:09,780 Maybe I'll move that up a little bit. 81 00:04:10,350 --> 00:04:13,860 Make this a little bigger play. 82 00:04:19,160 --> 00:04:20,480 And where's our step? 83 00:04:20,510 --> 00:04:21,420 There it is. 84 00:04:21,440 --> 00:04:23,810 And it is New Yeller. 85 00:04:23,840 --> 00:04:25,220 Pretty cool. 86 00:04:25,610 --> 00:04:26,030 All right. 87 00:04:26,030 --> 00:04:27,560 We're moving right along. 88 00:04:27,590 --> 00:04:29,730 Let's go ahead and make a whole bunch of steps. 89 00:04:29,750 --> 00:04:32,380 I'm going to get rid of this output window x. 90 00:04:32,390 --> 00:04:33,830 Let's make a whole bunch of steps. 91 00:04:33,830 --> 00:04:35,840 But we can't just keep putting numbers in here. 92 00:04:35,840 --> 00:04:37,030 That's bad programming. 93 00:04:37,040 --> 00:04:38,030 It's called literals. 94 00:04:38,030 --> 00:04:42,170 When you use the actual value, we need to use variables in that way. 95 00:04:42,170 --> 00:04:45,110 We don't have to calculate stuff up without the calculator. 96 00:04:45,110 --> 00:04:47,420 We can let the computer figure out where to put things. 97 00:04:47,960 --> 00:04:56,540 Let's say a local I'm going to call the position of the step, maybe y pause for y position. 98 00:04:56,900 --> 00:05:02,210 Make that zero z pause for z position. 99 00:05:02,240 --> 00:05:03,350 Make that zero. 100 00:05:03,380 --> 00:05:05,090 We're just initializing it to zero. 101 00:05:05,210 --> 00:05:11,330 Now I need a step height h t equals it's going to be two. 102 00:05:11,480 --> 00:05:16,550 Now notice that this is a capital H just like this was a capital N, right? 103 00:05:17,090 --> 00:05:25,940 When we do variables that are multi word we usually or multi word part, we usually capitalize the beginning 104 00:05:25,940 --> 00:05:26,930 of each new word. 105 00:05:26,930 --> 00:05:28,310 It's called camel case. 106 00:05:28,310 --> 00:05:33,140 It's for readability, but in Lua we are case sensitive. 107 00:05:33,140 --> 00:05:35,990 So if you put a lowercase h, you're going to get an error. 108 00:05:36,650 --> 00:05:37,880 Just keep it in mind, right? 109 00:05:37,970 --> 00:05:39,740 Do it exactly the way I'm doing it. 110 00:05:39,950 --> 00:05:41,990 Step distance. 111 00:05:42,020 --> 00:05:44,270 I'll say step dist three. 112 00:05:44,270 --> 00:05:46,970 That's the distance down the Z axis. 113 00:05:47,120 --> 00:05:48,680 All right, so we got these variables. 114 00:05:48,680 --> 00:05:49,670 What are we going to do with them? 115 00:05:49,760 --> 00:05:51,260 Let's come down here. 116 00:05:52,730 --> 00:05:57,920 After we wait 6 seconds to make our step, we've got to get rid of this two in this three. 117 00:05:58,360 --> 00:06:00,560 All right, so we do have a Y value. 118 00:06:00,590 --> 00:06:09,830 We're going to set y a new Y value using the old Y value plus step height. 119 00:06:10,850 --> 00:06:11,260 All right. 120 00:06:11,300 --> 00:06:12,840 So that was zero. 121 00:06:12,860 --> 00:06:17,210 This was a20 plus two equals two. 122 00:06:17,310 --> 00:06:17,890 Right. 123 00:06:18,050 --> 00:06:19,840 And then we're going to do the same with Z. 124 00:06:19,850 --> 00:06:22,160 Oh, these are Y position. 125 00:06:23,850 --> 00:06:24,660 Why? 126 00:06:24,660 --> 00:06:25,890 Pause. 127 00:06:27,330 --> 00:06:29,820 And this is the pause. 128 00:06:32,120 --> 00:06:32,950 There he goes. 129 00:06:33,290 --> 00:06:34,340 Pause. 130 00:06:34,670 --> 00:06:37,220 Plus step distance. 131 00:06:38,410 --> 00:06:39,190 Sweet. 132 00:06:39,640 --> 00:06:43,160 All right, now we can put this in here. 133 00:06:43,180 --> 00:06:43,780 Why? 134 00:06:43,810 --> 00:06:45,010 Pause. 135 00:06:46,670 --> 00:06:51,980 The pause, you're like, Man, that was a lot of extra work just to replace a two and a three. 136 00:06:52,280 --> 00:06:54,260 But let me tell you what's cool about that. 137 00:06:55,430 --> 00:06:56,720 We could do this. 138 00:06:58,110 --> 00:06:59,400 Control C. 139 00:07:00,820 --> 00:07:02,380 Control v. 140 00:07:02,570 --> 00:07:03,790 Let's do it again. 141 00:07:04,360 --> 00:07:05,830 Control v. 142 00:07:06,670 --> 00:07:09,220 And every time this is going to increase. 143 00:07:10,470 --> 00:07:11,400 Hit play. 144 00:07:14,140 --> 00:07:15,310 Let's see what we got. 145 00:07:18,020 --> 00:07:19,750 Three steps. 146 00:07:19,760 --> 00:07:23,570 Now, that is setting us up perfectly for a loop. 147 00:07:23,570 --> 00:07:25,070 For a for loop. 148 00:07:25,460 --> 00:07:25,850 All right. 149 00:07:25,850 --> 00:07:28,040 We can get rid of these because we're going to use a loop. 150 00:07:28,040 --> 00:07:29,590 We don't have to keep pasting code. 151 00:07:29,600 --> 00:07:31,310 That's what we're trying to get away from. 152 00:07:32,090 --> 00:07:32,390 All right. 153 00:07:32,390 --> 00:07:33,530 So how do we do a loop? 154 00:07:34,100 --> 00:07:35,210 Let's go right after this. 155 00:07:35,240 --> 00:07:36,650 Wait 6 seconds. 156 00:07:36,710 --> 00:07:40,130 Now, for loops are a type of loop that are really good. 157 00:07:40,130 --> 00:07:43,820 If you know how many times you want to execute the loop. 158 00:07:43,850 --> 00:07:45,580 I want to make ten steps. 159 00:07:45,590 --> 00:07:49,250 I know that the for loop has to run ten times a while. 160 00:07:49,250 --> 00:07:55,130 Loop, which we'll do in another video, will run an indeterminate period of time and maybe forever. 161 00:07:55,140 --> 00:07:55,640 Right? 162 00:07:55,640 --> 00:07:58,430 So we'll say for or for loop. 163 00:07:58,430 --> 00:08:00,010 We need a loop counter. 164 00:08:00,020 --> 00:08:01,370 I'm going to call it I. 165 00:08:01,400 --> 00:08:03,280 You can call it whatever you want. 166 00:08:03,290 --> 00:08:04,820 I'm going to set it to one. 167 00:08:05,180 --> 00:08:13,100 Then when I gets to ten, I'm going to break the loop and then I'm going to increase I every loop iteration 168 00:08:13,370 --> 00:08:14,660 by one. 169 00:08:15,080 --> 00:08:15,710 Right? 170 00:08:15,710 --> 00:08:23,000 So this is the start, this is the stop and this is the increased value per loop of I. 171 00:08:23,630 --> 00:08:32,720 Q Let's get this control x, control V and then let's print out I. 172 00:08:33,320 --> 00:08:39,380 So I'll do a comma after C name quote I equals put a space there. 173 00:08:40,940 --> 00:08:44,720 I know that we can see what I is doing in the loop. 174 00:08:45,290 --> 00:08:46,250 Let's run it. 175 00:08:48,790 --> 00:08:51,400 Oh, let's see our view output. 176 00:08:55,260 --> 00:08:56,210 There we go. 177 00:08:56,220 --> 00:08:56,780 Look at that. 178 00:08:56,790 --> 00:08:58,430 We got all of our colors. 179 00:08:58,440 --> 00:09:02,160 Here's eye as one, two, three, four, five. 180 00:09:02,160 --> 00:09:05,340 Just like we thought we could go up the steps. 181 00:09:05,880 --> 00:09:07,290 Pretty cool. 182 00:09:08,190 --> 00:09:10,130 So that's your first introduction to Loops? 183 00:09:10,470 --> 00:09:12,930 You know how to do return values from functions. 184 00:09:12,960 --> 00:09:14,070 Next video. 185 00:09:14,070 --> 00:09:20,970 I want to do a little click detector that fires this off and makes steps that would be cool. 186 00:09:21,000 --> 00:09:23,160 I will see you in the next video.